<?php
/**
 * This file is part of DomSql.
 * 
 * DomSql is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomSql is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomSql.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* Class to handle mysql databases.
* 
* This class is not complete and may change without warning!
* 
* @package DomSql
*/ 
class DomSqlDatabase {
	
	/**
	 * @var String
	 */
	private $name;
	
	/**
	 * @param String $name
	 */
	public function __construct( $name ) {
		$this->name = $name;
	}	
	/**
	 * Creates database
	 * 
	 * @return Bool
	 */
	public function create() {
		try {
			DomSql::query('CREATE DATABASE `'.$this->name.'`');
		} catch( RuntimeException $e ) {
			throw $e;
		}
	}
	/**
	 * Drops database
	 * 
	 * @return Bool
	 */
	public function drop() {
		try {
			DomSql::query('DROP DATABASE `'.$this->name.'`');
		} catch( RuntimeException $e ) {
			throw $e;
		}
	}
	/**
	 * Deletes all tables in database
	 *
	 * @return Bool
	 */
	public function deleteTables() {
		foreach( $this->getTables() as $table ) {
			$table->drop();
		}
		return true;
	}
	/**
	 * Returns all tables in database
	 *
	 * @return DomSqlTable[]
	 */
	public function getTables() {
		$tables = array();
		foreach( DomSql::selectQuery('SHOW TABLES FROM '.$this->name.'') as $table ) {
			$tables[] = new DomSqlTable( $table['Tables_in_'.strtolower($this->name)] );
		}
		return $tables;
	}
	/**
	 * @param String $var
	 * @return Mixed
	 */
	public function __get( $var ) {
		return $this->$var;
	}
	
}
?>